<template>
  <div class="content_box">
    <div class="top_box" :style="{height: navBarHeight + 'px'}">
      <div class="box">
        <div class="nav-statusbar" :style="{height: statusBarHeight + 'px'}"></div>
        <div class="back_box" :style="{height: titleBarHeight + 'px'}">
          <!-- <img src="/static/images/login/login.jpg" alt=""> -->
          门店选择
        </div>
        <img class="bj" src="https://tpp-admin.oss-cn-beijing.aliyuncs.com/rich/20230411/UuLCO7DKACibEYTxGCix.png" alt="">
      
        <div class="search_box">
          <div class="search">
            <div class="city">
              <p class="name">重庆市</p>
              <img src="/static/images/sjx1.png" alt="">
            </div>
            <input type="text" placeholder="搜索店铺">
          </div>
          <div class="icon" @click="clickChange">
            <img src="/static/images/map.png" alt="">
            <p class="text">地图</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 列表 -->
    <div>
      <!-- 房间列表 -->
      <div class="list_nr_box">
        <block v-if="markers.length">
          <div class="list_box">
            <div class="box" @click="toRoom(item)" v-for="(item,index) in markers" :key="index">
              <div class="img">
                <van-image
                  width="100%"
                  height="100%"
                  radius="16rpx"
                  lazy-load
                  :src="item.storeImg&&item.storeImg.length?item.storeImg[0]:''"
                />
                <p class="lb">24小时营业</p>
                <!-- <div class="mc">
                  <div>
                    <p>24小时营业</p>
                    <p>空闲{{item.freeCount}}间</p>
                  </div>
                </div> -->
              </div>
              <div class="msg">
                <div class="name_box">
                  <p class="name">{{item.storeName}}</p>
                  <!-- <p class="wz"><span>{{item.distance}}</span><van-icon name="arrow" /></p> -->
                  <p class="dz"><img src="/static/images/dz.png" alt="">{{item.address}}</p>
                </div>
                <!-- <p class="dz">{{item.address}}</p> -->
                <div class="wz_box">
                  <p class="tip" v-if="item.latestOrderFen">{{item.latestOrderFen}}前有人预定</p>
                  <div class="dw">
                    <p class="label">距我{{item.distance}}</p>
                    <div class="dh_icon">
                      <img @click.stop="onGuideTap(item)" src="/static/images/store_dh.png" alt="">
                      <img @click.stop="kefuPop" src="/static/images/store_tell.png" alt="">
                    </div>
                    <p class="anniu">去预定</p>
                  </div>
                </div>
              </div>
              <!-- <div class="bottom_box">
                <p class="button">剩余{{item.freeCount}}个可用</p>
                <p class="button sy">已使用{{item.useCount}}个房间</p>
              </div> -->
            </div>
          </div>
        </block>
        <div class="no_data" v-else>
          <img mode="widthFix" src="/static/images/components/no_sp.png" alt="" />
          <p class="text">暂无搜索店铺</p>
      </div>
      </div>
      
      <!-- 联系客服 -->
      <div class="kefu_pop">
        <van-popup position="bottom" :show="kefuShow" @close="kefuShow = false">
          <div class="box">
            <p class="title">联系客服</p>
            <div class="nr">
              <button type="primary" open-type="contact">
                <p class="img">
                  <img src="/static/images/kefu.png" alt="">
                </p>
                <p class="text">在线客服</p>
              </button>
              <button @click="callPhone">
                <p class="img">
                  <img src="/static/images/tell.png" alt="">
                </p>
                <p class="text">电话客服</p>
              </button>
            </div>
            <p class="quxiao" @click="kefuShow = false">取消</p>
          </div>
        </van-popup>
      </div>
    </div>
  </div>
</template>

<script>
import { 
  getToken,
  setLatitude,
  setLongitude,
  getUserPhone,
} from "@/utils/auth";
import { 
  searchNearby,
} from "@/api/index";
export default {
  data() {
    return {
      statusBarHeight: "", // 状态栏高度
      titleBarHeight: "", // 标题栏高度
      navBarHeight: "", // 导航栏总高度
      markers:[],
      userMsg:{},
      kefuShow:false,
      param:{
        current:1,
        size:10,
        name:'',
        latitude: 39.9086008701382, //纬度
        longitude: 116.3975661910453, //经度
        storeName:'',
        count:10,
        distance:100,
        storeType:0
      },
      isBack:'0'
    };
  },
  methods: {

    // 客服点击
    kefuPop(){
      this.kefuShow = true
    },

    // 获取地图标点
    getSearchNearby(){
      searchNearby(this.param).then(res=>{
        if (res.statusCode == '00000') {
          this.markers = []
          res.data.forEach(item => {
            if(item.latestOrderFen){
              if(item.latestOrderFen < 60){
                item.latestOrderFen = item.latestOrderFen+'分钟'
              }else{
                item.latestOrderFen = parseInt((item.latestOrderFen/60))+'小时'
              }
            }
            if(item.distance >= 1000){
              item.distance = (item.distance/1000).toFixed(1)+'km'
            }else{
              item.distance = item.distance+'m'
            }
            this.markers = res.data
          });
          wx.hideLoading()
        }else{
          wx.hideLoading()
        }
      })
    },
    
    // 先获取经纬度
    getMyLocation(){
      const _this = this;
      wx.getLocation({
        type:'gcj02',
        success(res) {
          _this.param.latitude = res.latitude;
          _this.param.longitude = res.longitude;
          //获取地图标点
          _this.getSearchNearby()

          // 存经纬度
          setLatitude(res.latitude)
          setLongitude(res.longitude)
        },
        fail: function () {
          wx.showModal({
            title: '开启位置授权',
            content: '无法获取当前定位，请开启位置授权',
            confirmText: "去开启",
            success: function(res) {
              if (res.confirm) {
                wx.getSetting({
                  success(res) {
                    if (res.authSetting['scope.userLocation'] == false){// 如果已拒绝授权，则打开设置页面
                      wx.openSetting({
                        success(res) {
                          wx.getLocation({
                            type:'gcj02',
                            success(res) {
                              console.log(res)
                              _this.param.latitude = res.latitude;
                              _this.param.longitude = res.longitude;
                              //获取地图标点
                              _this.getSearchNearby()

                              // 存经纬度
                              setLatitude(res.latitude)
                              setLongitude(res.longitude)
                            }
                          })
                        }
                      })
                    }  else { // 第一次授权，或者已授权，直接调用相关api
                      wx.openSetting({
                      success: (res) => { }
                    })
                      // that.getMyLocation()
                    }
                  }
                })
              } else if (res.cancel) {
              console.log('用户点击取消')
              }
            }
          })
        }
      });
    },

    //跳转店铺房间
    toRoom(item) {
      if(!getUserPhone()){
        wx.navigateTo({
          url: '/pages/authorization/main',
        });
        return
      }
      this.$store.commit('SET_STOREID',item.storeId)
      if(this.isBack == '1'){
        wx.navigateBack({ 
          delta: 1, 
        })
        return
      }
      wx.switchTab({
        url: `/pages/store_index/main`,
      });
      
    },

    //导航
    onGuideTap: function (info) {
      wx.openLocation({
        address: info.address,
        name: info.storeName,
        longitude: +info.longitude,
        latitude: +info.latitude,
        scale: 16
      })
    },

    // 切换
    clickChange(){
      this.$toast('地图暂未开放！')
      // if(this.isMap){
      //   this.isMap = false
      //   return
      // }
      // this.isMap = true
    },
    // 客服电话
    callPhone() {
      wx.makePhoneCall({
        phoneNumber: this.userMsg.mobile //仅为示例，并非真实的电话号码
      })
    },
  },
  beforeMount() {
    const self = this;
    wx.getSystemInfo({
      success(system) {
        console.log(`system:`, system);
        self.statusBarHeight = system.statusBarHeight;
        let platformReg = /ios/i;
        if (platformReg.test(system.platform)) {
          self.titleBarHeight = 44;
        } else {
          self.titleBarHeight = 48;
        }
        self.navBarHeight = self.statusBarHeight + self.titleBarHeight;
      },
    });
  },
  onShow(){
   if(!getToken()){
				wx.login({
					success:(res)=>{
						let code = res.code
						this.$store.dispatch("userLogin", code).then(res=>{
							if(res.statusCode == '00000'){
							}
						})
					}
				})
			}
  },
  onLoad(options) {
    if(options.isBack){
      this.isBack = options.isBack
    }
    // this.getIsStore()
    this.getMyLocation()

    if (wx.canIUse("getUpdateManager")) {
      console.log("进去版本检测------->");
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        if (res.hasUpdate) {
          //下载成功
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: "更新提示",
              content: "新版本已经准备好，是否重启应用？",
              success: function (res) {
                if (res.confirm) {
                  updateManager.applyUpdate();
                }
              },
            });
          });
          //下载失败
          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: "已经有新版本了哟~",
              content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~",
            });
          });
        } else {
          console.log("最新版本-------》");
        }
      });
    } else {
      wx.showModal({
        title: "提示",
        content:
          "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。",
      });
    }
  },
  onUnload() {
  },
  onShareAppMessage() {
    return {
      title: '我在老地方等你，速来~~',
      path: `/pages/index/main`,
    }
 },
};
</script>

<style lang="scss" scoped>
.content_box {
  min-height: 100vh;
  background: #f5f5f5;
  .top_box {
    .box{
      background: #fff;
      position: fixed;
      top:0;
      z-index: 100;
      width: 100%;
      .bj{
        width: 100%;
        height: 104%;
        position: absolute;
        top:0;
        z-index: 0;
      }
      .back_box {
        display: flex;
        align-items: center;
        font-weight: bold;
        font-size:32rpx;
        position: relative;
        z-index: 10;
        width: 100%;
        margin-left:30rpx;
        img{
          width: 40rpx;
          height: 40rpx;
          border-radius: 50%;
          margin:0 20rpx 0 30rpx;
        }
      }
      .search_box{
        position: relative;
        z-index: 10;
        margin:0 30rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .search{
          width: 598rpx;
          height: 68rpx;
          background: #FFFFFF;
          border-radius: 36rpx 36rpx 36rpx 36rpx;
          display: flex;
          align-items: center;
          .city{
            margin-left:30rpx;
            color:#222222;
            font-size:28rpx;
            display: flex;
            align-items: center;
            img{
              width: 19rpx;
              height: 8rpx;
              margin-left:8rpx;
            }
          }
          input{
            font-size:28rpx;
            margin-left:30rpx;
          }
        }
        .icon{
          text-align: center;
          img{
            width: 42rpx;
            height: 41rpx;
          }
          .text{
            font-size:24rpx;
          }
        }
      }
    }
    
  }
  .more_store_box{
    .conduct_box{
      height: 100rpx;
      background: #fff;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size:28rpx;
      color:#222;
      margin-top:-16rpx;
      font-weight: 500;
      .text{
        display: flex;
        align-items: center;
        img{
          width: 32rpx;
          height: 32rpx;
          margin-right:10rpx;
        }
      }
      span{
        color:red;
      }
      .anniu{
        width: 150rpx;
        height: 60rpx;
        background: #2D8EF5;
        border-radius: 50rpx;
        text-align: center;
        line-height: 60rpx;
        font-size:26rpx;
        color:#fff;
      }
    }
    .map_content_box{
      // background-image: linear-gradient(#b2c076 10%, transparent 60%);
      padding-top:30rpx;
    }
    .map_box{
      width: 90%;
      padding:16rpx;
      background: #fff;
      border-radius: 20rpx;
      margin:0 auto;
      box-shadow: 0rpx 0rpx 20rpx rgba(0,0,0,0.1);
      .map{
        height: 54vh;
        width: 100%;
        border-radius: 20rpx;
        overflow: hidden;
        position: relative;
        z-index: 50;
        #map{
        width: 100%;
        height: 100%;
        position: relative;
        z-index: 10;
        }
        .dq{
          position: absolute;
          top: 39%;
          right:46%;
          z-index: 120;
          img{
            width: 50rpx;
            height: 94rpx;
          }
        }
        .no_store{
          position: absolute;
          top: 34%;
          right:30%;
          z-index: 120;
          .top{
            display: flex;
            align-items: center;
            background: #4A4B5D;
            padding:10rpx 30rpx 10rpx 10rpx;
            border-radius: 100rpx;
            color:#fff;
            font-size:28rpx;
            font-weight: 500;
            .gth{
              border:6rpx solid #FC545C;
              width: 40rpx;
              height: 40rpx;
              border-radius: 50%;
              margin-right:15rpx;
              .box{
                background: #fff;
                border-radius: 50%;
                height: 100%;
                width: 100%;
                text-align: center;
                font-size:36rpx;
                display:flex;
                align-items: center;
                justify-content: center;
                font-weight: bold;
                color:#4A4B5D;
              }
            }
          }
          .line{
            width: 6rpx;
            height: 60rpx;
            background: #4A4B5D;
            margin:0 auto;
          }
        }
        .store_msg_box{
          position: absolute;
          bottom: 30rpx;
          left:30rpx;
          z-index: 120;
          background: #fff;
          width: 92%;
          border-radius: 15rpx;
          .top{
            padding:20rpx;
            border-bottom:1px solid #f9f9f9;
            display: flex;
            align-items: center;
            .img{
              width: 120rpx;
              height: 120rpx;
              border-radius: 20rpx;
              margin-right:30rpx;
            }
            .msg{
              flex: 1;
              width: 50%;
            }
            .name_box{
              display: flex;
              justify-content: space-between;
              align-items: center;
              margin-bottom:10rpx;
              .name{
                font-size:30rpx;
                color:#222;
                font-weight: bold;
              }
              .wz{
                color:#999;
                font-size:24rpx;
                span{
                  margin-right:10rpx;
                }
              }
            }
            .time{
              font-size:24rpx;
              color:#666;
              margin-bottom:10rpx;
            }
            .dz{
              font-size:24rpx;
              color:#666;
              white-space:nowrap;
              overflow:hidden;
              text-overflow:ellipsis;
            }
          }
          .bottom_box{
            padding:0 30rpx;
            height: 80rpx;
            line-height: 80rpx;
            font-size:28rpx;    
          }
        }
        .icon_box{
          position: absolute;
          top: 20rpx;
          right:20rpx;
          z-index: 120;
          .icon{
            width: 70rpx;
            height: 70rpx;
            background: rgba(255, 255, 255, 0.9);
            border-radius: 50rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0rpx 6rpx 8rpx 1rpx #ddd;
            margin-bottom:40rpx;
            img{
              width: 42rpx;
              height: 42rpx;
              
            }
          }
        }
      }
      .lanmu_box{
        display: flex;
        align-items: center;
        text-align: center;
        justify-content: space-between;
        padding:30rpx 20rpx 0rpx 20rpx;
        font-size:26rpx;
        img{
          width: 60rpx;
          height: 60rpx;
          margin-bottom:6rpx;
        }
        .text{
          
        }
      }
    }
    .banner_box{
      width: 94%;
      margin:0 auto;
      height: 200rpx;
      margin:30rpx auto;
      position: relative;
      border-radius: 20rpx;
      overflow: hidden;
      z-index: 50;
      .swiper{
        height: 200rpx;
        position: relative;
        z-index: 10;
        /deep/ .van-image{
          border-radius: 20rpx;
        }
      }
      .dots{
        width: 100%;
        position: absolute;
        left:0;
        bottom:10rpx;
        display: flex;
        justify-content: center;
        z-index: 60;
        p{
          width: 8rpx;
          height: 8rpx;
          background: rgba(0, 0, 0, 0.1);
          margin-left:10rpx;
          &.active{
              width: 20rpx;
              background: #b2c076;
          }
        }
      }
    }
    .kefu_pop{
      /deep/ .van-popup {
        border-top-right-radius: 20rpx !important;
        border-top-left-radius: 20rpx !important;
      }
      .title{
        height: 120rpx;
        line-height: 120rpx;
        font-size:34rpx;
        color:#505050;
        font-weight: 600;
        text-align: center;
      }
      .nr{
        display: flex;
        align-items: center;
        justify-content: space-around;
        padding-bottom:48rpx;
      }
      button::after {
        border: none;
      }
      button{
        background: none;
        color:#222;
        margin:0;
        .img{
          width: 100rpx;
          height: 100rpx;
          background: #F9F9FB;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          img{
            width: 52rpx;
            height: 52rpx;
          }
        }
        .text{
          color:#6C6C6C;
          font-size:24rpx;
          text-align: center;
        }
      }
      .quxiao{
        margin:0 30rpx;
        height: 96rpx;
        line-height: 96rpx;
        background: #222;
        border-radius: 4rpx;
        margin-bottom:24rpx;
        text-align: center;
        font-size:30rpx;
        color:#fff;
      }
    }
    .content_list_box{
      width: 94%;
      margin:0 auto;
      .title_box{
        display: flex;
        align-items: baseline;
        justify-content: space-between;
        .title{
          font-size:34rpx;
          display: flex;
          align-items: center;
          .line{
            width: 3px;
            height: 30rpx;
            background: #2D8EF5;
            margin-right:10rpx;
          }
        }
        .text{
          font-size:28rpx;
          color:#666
        }
      }
      .list_box{
        display: flex;
        flex-wrap:wrap;
        justify-content: space-between;
        margin-top:30rpx;
        .box{
          display: inline-block;
          width: 48.6%;
          background: #fff;
          border-radius: 20rpx;
          margin-bottom:20rpx;
          .img{
            width: 100%;
            height: 350rpx;
            border-top-right-radius: 20rpx;
            border-top-left-radius: 20rpx;
            overflow: hidden;
            img{
              // width: ;
            }
          }
          .msg{
            padding:0 20rpx;
            .name{
              font-size:30rpx;
              font-weight: bold;
              margin:10rpx 0;
            }
            .label_box{
              margin-top:10rpx;
              p{
                display: inline-block;
                margin-right:10rpx;
                font-size:22rpx;
                border:1px solid #fd8b8b;
                padding:2rpx 10rpx;
                border-radius: 8rpx;
                color:#f8482d;
                &:last-child{
                  margin-right:0;
                }
              }
            }
            .dz{
              font-size:24rpx;
              color:rgb(134, 133, 133);
              padding:10rpx 0 20rpx 0;
            }
          }
        }
      }
    }
  }

.kefu_pop{
    /deep/ .van-popup {
      border-top-right-radius: 20rpx !important;
      border-top-left-radius: 20rpx !important;
    }
    .title{
      height: 120rpx;
      line-height: 120rpx;
      font-size:34rpx;
      color:#505050;
      font-weight: 600;
      text-align: center;
    }
    .nr{
      display: flex;
      align-items: center;
      justify-content: space-around;
      padding-bottom:48rpx;
    }
    button::after {
      border: none;
    }
    button{
      background: none;
      color:#222;
      margin:0;
      .img{
        width: 100rpx;
        height: 100rpx;
        background: #F9F9FB;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        img{
          width: 52rpx;
          height: 52rpx;
        }
      }
      .text{
        color:#6C6C6C;
        font-size:24rpx;
        text-align: center;
      }
    }
    .quxiao{
      margin:0 30rpx;
      height: 96rpx;
      line-height: 96rpx;
      background: #222;
      border-radius: 4rpx;
      margin-bottom:24rpx;
      text-align: center;
      font-size:30rpx;
      color:#fff;
    }
  }
}

/*列表*/

  .list_nr_box {
    margin: 130rpx auto 0 auto;
    width: 94%;
    .list_box {
    .box{
      background: #fff;
      border-radius: 10rpx;
      margin-bottom:26rpx;
      display: flex;
      padding:26rpx;
      .img{
          width: 230rpx;
          height: 268rpx;
          border-radius: 16rpx;
          margin-right:30rpx;
          position: relative;
          .lb{
            position: absolute;
            top:0;
            left: 0;
            width: 146rpx;
            height: 46rpx;
            line-height: 46rpx;
            background: #8FB162;
            border-radius: 16rpx 0rpx 16rpx 0rpx;
            font-size: 24rpx;
            color:#fff;
            text-align: center;
          }
        }
        .msg{
          flex: 1;
          display: flex;
          flex-direction:column;
          justify-content: space-between;
          .wz_box{
            padding-bottom:4rpx;
          }
        }
        .name_box{
          .name{
            font-size:36rpx;
            color:#292929;
            font-weight: bold;
          }
          .wz{
            color:#999;
            font-size:24rpx;
            span{
              margin-right:10rpx;
            }
          }
        }
        .time{
          font-size:28rpx;
          color:#222;
          margin-bottom:15rpx;
        }
        .dz{
          width: 100%;
          font-size:24rpx;
          color:#666;
          margin-top:20rpx;
          overflow: hidden;
          -webkit-line-clamp: 2;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          img{
            width: 28rpx;
            height: 28rpx;
            margin-right:6rpx;
            position: relative;
            top:4rpx;
          }
        }
        .tip{
          color:#8FB162;
          font-size:26rpx;
        }
        .dw{
          display: flex;
          align-items: center;
          margin-top:30rpx;
          .label{
            padding:2rpx 6rpx;
            border: 1px solid #8FB162;
            font-size:24rpx;
            color:#8FB162;
            border-radius: 8rpx 8rpx 8rpx 8rpx;
          }
          .dh_icon{
            display: flex;
            img{
              width: 44rpx;
              height: 44rpx;
              margin-left:22rpx;
            }
          }
          .anniu{
            width: 100rpx;
            height: 49rpx;
            background: #8FB162;
            border-radius: 12rpx 12rpx 12rpx 12rpx;
            font-size:26rpx;
            color:#fff;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-left:auto;
          }
        }
      .bottom_box{
        padding:0 30rpx;
        height: 80rpx;
        display: flex;
        align-items: center;
        .left{
          display: flex;
        }
        .text{
          display: flex;
          align-items: center;
          margin-right:40rpx;
          font-size:26rpx;
          &:last-child{
            margin-right: 0;
          }
          img{
            width: 30rpx;
            height: 30rpx;
            margin-right:10rpx;
          }
        }
        .button{
          padding:0 10rpx;
          height: 42rpx;
          font-size:26rpx;
          border-radius: 10rpx;
          background: #e2f1fd;
          color:#1499ff;
          display: flex;
          align-items: center;
          justify-content: center;
          &.on{
            background: #eefae0;
            color:#92b46c
          }
        }
        .sy{
          color:#d47a39;
          background: #fcf6f1;
          margin-left:20rpx;
        }
      }
    }
  }
  }
  .no_data{
    text-align: center;
    font-size:28rpx;
    color:#333;
    padding-top:150rpx;
    .text{
      font-weight: 500;
      color:#999;
    }
    img{
      width: 248rpx;
      margin-bottom:10rpx;
    }
  }
</style>